package com.mooer.app.service;

import java.io.IOException;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.mooer.MooerApplication;
import com.mooer.domain.ResBean;
import com.mooer.domain.ServiceUrl;
import com.mooer.http.HttpUtils;
import com.mooer.utils.UtilTools;

/**
 * 进行定位的相关操作
 * 
 * @author pengfei.xu
 * @version V1.0 创建时间：2011-4-29 上午09:32:40
 */
public class LocationService extends Service implements LocationListener{
	
	private static final String TAG = "LocationService";

	private Location location;

	private LocationManager locationManager;

	private Criteria criteria;
	
	private String provider = null;
	
	private MooerApplication application;
	
	private double latitude,longitude;

	public final static double latitude_change = -0.00186416629791;

	public final static double longitude_change = 0.00491628341675;

	@Override
	public IBinder onBind(Intent intent) {
		Log.i(TAG, "locationService onBind");
		return mBinder;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		application = (MooerApplication)getApplication();
		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);// 设置为最大精度
		criteria.setAltitudeRequired(false);// 不要求海拔信息
		criteria.setBearingRequired(false);// 不要求方位信息
		criteria.setCostAllowed(true);// 是否允许付费
		criteria.setPowerRequirement(Criteria.POWER_LOW);// 对电量的要求
		
		provider = locationManager.getBestProvider(criteria, true);
		Log.i(TAG, "provider:" + provider);
		location = locationManager.getLastKnownLocation(provider);
		if (location != null) {
			latitude = UtilTools.round(location.getLatitude(), 5); // 纬度
			longitude = UtilTools.round(location.getLongitude(), 5); // 经度
		} else {
			Log.i(TAG, "GPS有问题啊，location一直为null...");
			provider = LocationManager.NETWORK_PROVIDER;
			location = locationManager
					.getLastKnownLocation(provider);
			if(location == null) return;
			latitude = UtilTools.round(location.getLatitude(), 5); // 纬度
			longitude = UtilTools.round(location.getLongitude(), 5); // 经度
		}
		if (!(latitude == 0.0 && longitude == 0.0)) {
			Log.i(TAG,"before:" + latitude + "," + longitude);
//			latitude = latitude + latitude_change;
//			longitude = longitude + longitude_change;
			application.setLatitude(latitude);
			application.setLongitude(longitude);
			Log.i(TAG,"after:" + latitude + "," + longitude);
			new GetDetailLocation().execute(latitude, longitude);
			//监听位置变化，10秒一次，距离10米以上
			locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, this);
		}
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}
	
	@Override
	public void onDestroy() {
		Log.i(TAG, "locationService onDestroy");
		super.onDestroy();
		locationManager.removeUpdates(this);
	}

	public class GetDetailLocation extends AsyncTask<Double, Integer, String> {

		private boolean isException;
		
		@Override
		protected String doInBackground(Double... arg0) {
			String url = ServiceUrl.LOCATION_URL + arg0[0] + "," + arg0[1];
			ResBean resBean = HttpUtils.getStream(url);
			Log.i(TAG,resBean.getStatus()+"");
			String json = resBean.getData();
			if (json != null) {
				Log.i(TAG,json);
				try {
					JSONObject jsonObj = new JSONObject(json);
					JSONArray jsonArray = jsonObj.getJSONArray("Placemark");
					for (int i = 0; i < jsonArray.length(); i++) {
						jsonObj = jsonArray.getJSONObject(i);
//						String address = jsonObj.getString("address");
						jsonObj = jsonObj.getJSONObject("AddressDetails").getJSONObject("Country")
						.getJSONObject("AdministrativeArea").getJSONObject("Locality");
						String city = jsonObj.getString("LocalityName");
						String region = jsonObj.getJSONObject("DependentLocality").getString("DependentLocalityName");
						String road = jsonObj.getJSONObject("DependentLocality").getJSONObject("Thoroughfare").getString("ThoroughfareName");
						String address = city+region+road;
						Log.i(TAG,"[address][" + address + "]");
						application.setAddress(address);
						
					}
				} catch (JSONException e) {
					Log.e(TAG, "", e);
					isException = true;
				}
			}else{
				isException = true;
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
//			if(isException){
//				Geocoder geocoder = new Geocoder(LocationService.this);
//				try {
//					List<Address> list = geocoder.getFromLocation(latitude, longitude, 5);
//					String address = list.get(0).getAddressLine(0)+list.get(0).getAddressLine(1)+list.get(0).getAddressLine(2);
//					System.out.println("address:"+address);
//					application.setAddress(address);
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
		}

	}

	private IBinder mBinder = new LocationServiceBinder();

	public class LocationServiceBinder extends Binder {
		public LocationService getService() {
			return LocationService.this;
		}
	}
	
	public double getLatitude(){
		if(location!=null){
			return location.getLatitude();
		}
		return 0.0;
	}
	
	public double getLongitude(){
		if(location!=null){
			return location.getLongitude();
		}
		return 0.0;
	}

	@Override
	public void onLocationChanged(Location location) {
		Log.i(TAG, "<location changed>latitude:"+location.getLatitude()+";longitude:"+location.getLongitude());
		latitude = location.getLatitude() + latitude_change;
		longitude = location.getLongitude() + longitude_change;
		application.setLatitude(latitude);
		application.setLongitude(longitude);
		Log.i(TAG,"after:" + latitude + "," + longitude);
		new GetDetailLocation().execute(latitude, longitude);
	}

	@Override
	public void onProviderDisabled(String arg0) {
		Log.i(TAG,"<provider disabled>"+arg0);
		
	}

	@Override
	public void onProviderEnabled(String arg0) {
		Log.i(TAG, "<provider enabled>"+arg0);
		
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		Log.i(TAG, "<status changed>");
		
	}

}
